<template>
	<tm-fullView>
		<view class="pb-50 text grey " :class="$tm.vx.state().tmVuetify.black ? 'black bk' : ''">
			<tm-menubars :title="title" color="bg-gradient-primary-lighten" :showback="true"></tm-menubars>
			<tm-form @submit="submit" ref="formData" @request="success" method="post">
				<tm-sheet :shadow="24" :padding="[12,24]" :margin="[24,24]">
					<view class="mx-32 my-12 border-b-1  pb-12 flex-between"
						:class="$tm.vx.state().tmVuetify.black ? 'bk' : ''">
						<text class="text-size-n ">收入/支出</text>
						<tm-groupradio @change="shiguchechange">
							<tm-radio v-model="groupchecked.c_1" name="0" label="支出"></tm-radio>
							<tm-radio v-model="groupchecked.c_2" name="1" label="收入"></tm-radio>
						</tm-groupradio>
					</view>

					<tm-input name="value" required title="金额" input-type="digit" v-model="reqData.value"></tm-input>
					<tm-input name="remarks" title="备注" v-model="reqData.remarks"></tm-input>
					<tm-pickersDate @confirm="timeChange" :default-value="reqData.time">
						<tm-input disabled name="time" required title="时间" placeholder="请选择时间" :value="reqData.time"
							right-icon="icon-angle-right"></tm-input>
					</tm-pickersDate>
					<tm-input disabled name="type" required title="类型" :right-icon="type.icon" :value="type.text">
					</tm-input>
					<tm-grid :list="types" :grid="6" @change="selectType" color="orange"></tm-grid>
					<view class="px-24">
						<tm-button navtie-type="form" theme="bg-gradient-blue-accent" block>提交数据</tm-button>
					</view>
				</tm-sheet>
			</tm-form>
		</view>
	</tm-fullView>

</template>

<script>
	import tmFullView from "@/tm-vuetify/components/tm-fullView/tm-fullView.vue"
	import tmMenubars from "@/tm-vuetify/components/tm-menubars/tm-menubars.vue"
	import tmSheet from "@/tm-vuetify/components/tm-sheet/tm-sheet.vue"
	import tmForm from "@/tm-vuetify/components/tm-form/tm-form.vue"
	import tmActionSheet from "@/tm-vuetify/components/tm-actionSheet/tm-actionSheet.vue"
	import tmButton from "@/tm-vuetify/components/tm-button/tm-button.vue"
	import tmInput from "@/tm-vuetify/components/tm-input/tm-input.vue"
	import tmRate from "@/tm-vuetify/components/tm-rate/tm-rate.vue"
	import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
	import tmLoadding from "@/tm-vuetify/components/tm-loadding/tm-loadding.vue"
	import tmCheckbox from "@/tm-vuetify/components/tm-checkbox/tm-checkbox.vue"
	import tmGroupcheckbox from "@/tm-vuetify/components/tm-groupcheckbox/tm-groupcheckbox.vue"
	import tmSlider from "@/tm-vuetify/components/tm-slider/tm-slider.vue"
	import tmStepper from "@/tm-vuetify/components/tm-stepper/tm-stepper.vue"
	import tmRadio from "@/tm-vuetify/components/tm-radio/tm-radio.vue"
	import tmGroupradio from "@/tm-vuetify/components/tm-groupradio/tm-groupradio.vue"
	import tmPickersDate from "@/tm-vuetify/components/tm-pickersDate/tm-pickersDate.vue"
	import tmPickers from "@/tm-vuetify/components/tm-pickers/tm-pickers.vue"
	import tmPickersCity from "@/tm-vuetify/components/tm-pickersCity/tm-pickersCity.vue"
	import tmUpload from "@/tm-vuetify/components/tm-upload/tm-upload.vue"
	import tmSwitch from "@/tm-vuetify/components/tm-switch/tm-switch.vue"
	import tmGrid from '@/tm-vuetify/components/tm-grid/tm-grid.vue';
	export default {
		components: {
			tmFullView,
			tmSheet,
			tmMenubars,
			tmForm,
			tmActionSheet,
			tmButton,
			tmInput,
			tmRate,
			tmIcons,
			tmLoadding,
			tmGroupcheckbox,
			tmSlider,
			tmCheckbox,
			tmStepper,
			tmGroupradio,
			tmPickersDate,
			tmPickers,
			tmPickersCity,
			tmUpload,
			tmSwitch,
			tmRadio,
			tmGrid
		},
		data() {
			return {
				title: '',
				reqData: {
					id: '',
					er: 0,
					remarks: '',
					time: '',
					type: '',
					value: 0,
					index: 0
				},
				types: [],
				type: {},
				groupchecked: {
					c_1: true,
					c_2: false
				},

			}
		},
		onLoad(option) {
			let value = JSON.parse(decodeURIComponent(option.value));
			this.reqData = value
			this.title = value.remarks
			if (this.reqData.er == 1) {
				this.groupchecked = {
					c_1: false,
					c_2: true
				}
			} else {
				this.groupchecked = {
					c_1: true,
					c_2: false
				}
			}
			this.loadType()
		},
		mounted() {},
		computed: {},
		methods: {
			shiguchechange(e) {
				if ((e[0].name == '1' && e[0].checked) || (e[0].name == '0' && !e[0].checked)) {
					this.reqData.er = 1;
				} else {
					this.reqData.er = 0;
				}
			},
			loadType() {
				this.$ajax.get('/type/load/' + this.reqData.er).then(res => {
					this.types = res.data
				})
			},
			selectType(e) {
				this.type = e.data
				this.reqData.type = this.type.id
			},
			isNumber(val) {
				var regPos = /^[0-9]+$/; //非负浮点数
				var regNeg = /^[0-9]+\.?[0-9]+?$/; //负浮点数
				if (regPos.test(val) || regNeg.test(val)) {
					return true;
				} else {
					return false;
				}

			},
			submit(e) {
				if (e === false) {
					uni.$tm.toast("请填写必填项。")
				} else {
					if(this.isNumber(this.reqData.value)) {
						this.judgeLogin((res) => {
							this.reqData.uid = res.id
							this.$ajax.post('/bill/save', this.reqData).then(res => {
								if (res.code == 1) {
									uni.$tm.toast('记账成功');
									var item = this.reqData; //item为要传的值
									var pages = getCurrentPages();
									var prevPage = pages[pages.length - 2];
									prevPage.brand = item;
									uni.navigateBack()
								} else {
									uni.$tm.toast('记账失败');
								}
							})
						});
					}else {
						uni.$tm.toast("请填写合理值")
					}
				}

			},
			async getData(e) {
				let d = await this.$refs.formData.getFormData();
				uni.showModal({
					title: "数据如下",
					content: JSON.stringify(d),
					showCancel: false
				})
			},
			success(e) {
				//提交成功 。
				uni.$tm.toast(e.msg)
			},
			obTstr(o) {
				if (Array.isArray(o)) {
					if (o.length.length === 0) return "";
					return o.join(",")
				}
				return JSON.stringify(o)
			},
			obTstr2(o) {
				if (Array.isArray(o)) {
					if (o.length.length === 0) return "";
					return o[0]?.title ?? ""
				}
				return ''
			},
			timeChange(e) {
				this.$set(this.reqData, 'time', e.year + "-" + e.month + "-" + e.day)
			},
			shiguchechange(e) {
				if ((e[0].name == '1' && e[0].checked) || (e[0].name == '0' && !e[0].checked)) {
					this.reqData.er = 1;
				} else {
					this.reqData.er = 0;
				}
			},
		}
	}
</script>

<style>

</style>
